<template>
  <div  v-loading="loading">
    <el-button type="success" style="margin-bottom: 10px;" @click="record">录入信息</el-button>
    <el-button type="success" style="margin-bottom: 10px;" @click="deleteAll">清空菜单</el-button>
    <el-table
        :data="tableData"
        show-summary
        style="width: 100%">
      <el-table-column
          prop="dishName"
          label="菜品名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="dishType"
          label="菜品类型">
      </el-table-column>
      <el-table-column
          prop="fat"
          sortable
          label="脂肪(克)">
      </el-table-column>
      <el-table-column
          prop="carbohydrates"
          sortable
          label="碳水化合物(克)">
      </el-table-column>
      <el-table-column
          prop="protein"
          sortable
          label="蛋白质(克)">
      </el-table-column>
      <el-table-column
          prop="heat"
          sortable
          label="热量(千卡)">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.row)">详情
          </el-button>
          <el-button
              size="mini"
              @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import store from "@/store"
import request from "@/utils/request";
const username = store.state.app.username;
export default {
  data() {
    return {
      tableData: [],
      loading : true
    }
  },
  methods: {
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    },
    handleEdit(row) {
      this.$router.push({
        name: 'detail',
        params: {
          id: row.id,
        }
      })
    },
    handleDelete(row) {
      console.log(row)
      request.delete('menu/menuDelete/' + row.dishId)
          .then(res => {
            this.getMenu()
          }).catch(error => {
        console.log('error', error)
      })
    },
    record() {
      request.post('menu/addDishRecord', {
        dishInfo: JSON.stringify(this.tableData),
        username: username
      })
          .then(res => {
            alert("录入成功")
          }).catch(error => {
        console.log(error)
      })
    },
    deleteAll() {
      request.delete('menu/deleteAll/' + username)
          .then(res => {
            alert("删除成功")
            this.getMenu()
          }).catch(error => {
        console.log(error)
      })
    },
    getMenu() {
      console.log(username)
      request.get('menu/getMenu/' + username)
          .then(res => {
            this.tableData = res.data.data
            this.loading = false
          }).catch(error => {
        console.log('error', error)
      })
    }
  },
  mounted() {
  this.getMenu()
  }
}
</script>
